package com.metaera.app.utils;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

/**
 * Date: 2019/9/19
 * Desc: 富文本字体工具
 */
public class SpannableStringHelper {

    public static SpannableStringHelper newBuilder() {
        return newBuilder("");
    }

    public static SpannableStringHelper newBuilder(CharSequence text) {
        return new SpannableStringHelper(text);
    }


    public static SpannableString matchText(int color, String content, String keyword) {
        SpannableString spannableString = new SpannableString(content);
        keyword = escapeExprSpecialWord(keyword);
        content = escapeExprSpecialWord(content);
        if (content.contains(keyword) && !TextUtils.isEmpty(keyword)) {
            try {
                Pattern pattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(spannableString);
                while (matcher.find()) {
                    spannableString.setSpan(
                            new ForegroundColorSpan(color),
                            matcher.start(),
                            matcher.end(),
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                    );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return spannableString;
    }


    public static String escapeExprSpecialWord(String keyword) {
        if (!TextUtils.isEmpty(keyword)) {
            String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|", "%"};
            for (String key : fbsArr) {
                if (keyword.contains(key)) {
                    keyword = keyword.replace(key, "\\" + key);
                }
            }
        }

        return keyword;
    }


    private int start, end;

    private @ColorInt int backgroundColor = Color.WHITE, color = Color.BLACK;

    private final SpannableStringBuilder style;

    private SpannableStringHelper(CharSequence text) {
        style = new SpannableStringBuilder(text);
    }

    public SpannableStringHelper append(CharSequence text) {
        this.start = style.length();
        style.append(text);
        return range(start);
    }

    public SpannableStringHelper range(int start) {
        return this.range(start, style.length());
    }

    public SpannableStringHelper range(int start, int end) {
        this.start = start;
        this.end = end;
        return this;
    }

    public SpannableStringHelper backgroundColor(@ColorInt int backgroundColor) {
        this.backgroundColor = backgroundColor;
        style.setSpan(new BackgroundColorSpan(backgroundColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringHelper color(@ColorInt int color) {
        this.color = color;
        style.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringHelper click(View.OnClickListener clickable) {
        return click(this.start, this.end, clickable);
    }

    /**
     * 注意设置后要调用此方法
     * TextView.setMovementMethod(LinkMovementMethod.getInstance());
     */
    public SpannableStringHelper click(int start, int end, View.OnClickListener clickable) {
        ClickableSpan clickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                clickable.onClick(widget);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                //ds.setColor(color);
                ds.bgColor = backgroundColor;
                ds.setUnderlineText(false);
            }
        };

        style.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    // 百分比
    public SpannableStringHelper size(float proportion) {
        return size(proportion, start, end);
    }

    public SpannableStringHelper size(float proportion, int start, int end) {
        style.setSpan(new RelativeSizeSpan(proportion), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringHelper backgroundColor(@ColorInt int color, int start, int end) {
        style.setSpan(new BackgroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringHelper color(@ColorInt int color, int start, int end) {
        style.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringHelper bold() {
        return bold(start, end);
    }

    public SpannableStringHelper bold(int start, int end) {
        style.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringHelper drawable(Drawable drawable) {
        return drawable(drawable, start, end);
    }

    public SpannableStringHelper drawable(Drawable drawable, int start, int end) {
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        style.setSpan(new ImageSpan(drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringBuilder build() {
        return this.style;
    }


}
